﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HQWeb.SysManage.DepatmentManage
{
    public partial class DephartList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            try
            {
               
                if (!Page.IsPostBack)
                {
                    string strId = Request["pid"];
                    strId = strId == "all" ? "" : strId;
                    this.txtPIdHid.Text = strId;
                    BindList();
                }
            }
            catch (Exception ex)
            {
                //COM.HS.Utility.JScript.Alert(ex.Message);
                //return;
                throw new Exception(ex.Message);
            }
        }

        #region  参数检验
        private void check()
        {
            if (this.txtPIdHid.Text.Trim().Length == 0)
            {
                throw new Exception("参数传递出错！");
            }
        }
        #endregion

        #region 绑定列表数据
        private void BindList()
        {

            BLL.SysManage.T_PUB_DEPARTMENT bll = new BLL.SysManage.T_PUB_DEPARTMENT();
            string strNmae = this.txtName.Text.Trim();
            GridView1.DataSource = bll.GetNxtDepartMent(txtPIdHid.Text.Trim(), strNmae);
            GridView1.DataBind();
        }
        #endregion

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //strWhere = strWhere.Replace("order by operatetime DESC", "");
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                { // when click the "GO" Button
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    //GridViewRow pagerRow = GridView1.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {  // when click the first, last, previous and next Button
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }

                // check to prevent form the NewPageIndex out of the range


                BindList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 编辑删除

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCommand = e.CommandName;
            string strID = e.CommandArgument.ToString();
            if (strCommand.Equals("UpdateData"))  //编辑
            {
                Response.Redirect("DeaprtEdit.aspx?sign=1&id=" + strID);
            }
            else if (strCommand.Equals("DelData"))  //删除
            {
                //判断是否存在子部门
                BLL.SysManage.T_PUB_DEPARTMENT DePartBll = new BLL.SysManage.T_PUB_DEPARTMENT();
                List<Model.SysManage.T_PUB_DEPARTMENT> modelList = DePartBll.GetNxtDepartMent(strID, "");
                if (modelList.Count > 0)
                {
                    Universal.JS.Alert("存在下级部门！请先删除");
                    return;
                }
                BLL.SysManage.T_PUB_USER userBll = new BLL.SysManage.T_PUB_USER();
                List<Model.SysManage.T_PUB_USER> userModelList = userBll.GetModelListByDepart(strID);
                //判断部门下是否存在人员
                if (userModelList.Count > 0)
                {
                    Universal.JS.Alert("该部门已存在人员！请先删除");
                    return;
                }
                int intTemp;
                int.TryParse(strID, out intTemp);
                Model.SysManage.T_PUB_DEPARTMENT DepartModel = DePartBll.GetModel(intTemp);
                DePartBll.Delete(intTemp);
               
                if (DepartModel != null)
                {
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "删除" + DepartModel.DEPARTMENT_NAME + "部门", DateTime.Now);
                }

                BindList();
            }
        }

        #endregion
        //查询
        protected void tbnSearch_Click(object sender, EventArgs e)
        {
            BindList();
        }
        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strUrl = string.Format("DeaprtEdit.aspx?sign=0&pid={0}", txtPIdHid.Text);
            Response.Redirect(strUrl);
        }
    }
}